package com.xxx.chat.server.handler;

import com.xxx.chat.message.GroupMembersRequestMessage;
import com.xxx.chat.message.GroupMembersResponseMessage;
import com.xxx.chat.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Set;

/**
 * 获取群组成员请求处理handler
 * SimpleChannelInboundHandler<T>可以只监听感兴趣的类型的消息
 */
@ChannelHandler.Sharable
public class GroupMembersRequestMessageHandler extends SimpleChannelInboundHandler<GroupMembersRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMembersRequestMessage msg) throws Exception {
        // 获取群会话列表中所有成员用户名
        Set<String> members = GroupSessionFactory.getGroupSession().getMembers(msg.getGroupName());
        ctx.writeAndFlush(new GroupMembersResponseMessage(members));
    }
}
